package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicExpensesDeviceDTO;
import com.iwomy.secureplat.platform.dto.BasicExpensesDviceConfigDTO;
import com.iwomy.secureplat.platform.entity.BasicExpensesDviceConfigEntity;
import com.iwomy.secureplat.platform.entity.BasicInventoryListEntity;
import com.iwomy.secureplat.platform.service.BasicExpensesDeptDetailService;
import com.iwomy.secureplat.platform.service.BasicExpensesDviceConfigService;
import com.iwomy.secureplat.platform.service.BasicInventoryListService;
import com.iwomy.secureplat.platform.vo.BasicExpensesDviceConfigVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资费管理-设备配置及赠送记录
 *
 * @author pig
 * @date 2023-12-12 11:14:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicExpensesDviceConfig")
@Tag(description = "basicExpensesDviceConfig", name = "资费管理-设备配置及赠送记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicExpensesDviceConfigController {

	private final BasicExpensesDviceConfigService basicExpensesDviceConfigService;

	private final BasicExpensesDeptDetailService expensesDeptDetail;

	private final BasicInventoryListService basicInventoryList;

	/**
	 * 分页查询
	 *
	 * @param page                  分页对象
	 * @param basicExpensesDeviceDTO 资费管理-设备配置及赠送记录
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_view')" )
	public R getBasicExpensesDviceConfigPage(@ParameterObject Page page, @ParameterObject BasicExpensesDeviceDTO basicExpensesDeviceDTO) {
		return R.ok(basicExpensesDviceConfigService.getBasicInventoryListPage(page, basicExpensesDeviceDTO));
	}

	/**
	 * @param deviceId 根据设备id查看赠送记录
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/getDeliveryList/{deviceId}")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_view')")
	public R getDeliveryList(@PathVariable("deviceId") Long deviceId) {
		List<BasicExpensesDviceConfigVO> deliveryList = basicExpensesDviceConfigService.getDeliveryList(deviceId);
		deliveryList.forEach(expensesDevice -> {
			BasicInventoryListEntity basicInventoryListEntity = basicInventoryList.getInventoryList(deviceId);
			expensesDevice.setPlatformExpireDate(basicInventoryListEntity.getPlatformExpireDate());
		});
		return R.ok(deliveryList);
	}

	/**
	 * 通过id查询资费管理-设备配置及赠送记录
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicExpensesDviceConfigService.getById(id));
	}

	/**
	 * 新增资费管理-设备配置及赠送记录
	 *
	 * @param basicExpensesDviceConfigDTO 资费管理-设备配置及赠送记录
	 * @return R
	 */
	@Operation(summary = "新增资费管理-设备配置及赠送记录", description = "新增资费管理-设备配置及赠送记录")
	@SysLog("新增资费管理-设备配置及赠送记录")
	@PostMapping
//	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_add')")
	public R save(@RequestBody BasicExpensesDviceConfigDTO basicExpensesDviceConfigDTO) {

		return basicExpensesDviceConfigService.newlyAddedBatch(basicExpensesDviceConfigDTO);
	}

	/**
	 * 修改资费管理-设备配置及赠送记录
	 *
	 * @param basicExpensesDviceConfig 资费管理-设备配置及赠送记录
	 * @return R
	 */
	@Operation(summary = "修改资费管理-设备配置及赠送记录", description = "修改资费管理-设备配置及赠送记录")
	@SysLog("修改资费管理-设备配置及赠送记录")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_edit')")
	public R updateById(@RequestBody BasicExpensesDviceConfigEntity basicExpensesDviceConfig) {
		return R.ok(basicExpensesDviceConfigService.updateById(basicExpensesDviceConfig));
	}

	/**
	 * 通过id删除资费管理-设备配置及赠送记录
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除资费管理-设备配置及赠送记录", description = "通过id删除资费管理-设备配置及赠送记录")
	@SysLog("通过id删除资费管理-设备配置及赠送记录")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicExpensesDviceConfigService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicExpensesDviceConfig 查询条件
	 * @param ids                      导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicExpensesDviceConfig_export')")
	public List<BasicExpensesDviceConfigEntity> export(BasicExpensesDviceConfigEntity basicExpensesDviceConfig, Long[] ids) {
		return basicExpensesDviceConfigService.list(Wrappers.lambdaQuery(basicExpensesDviceConfig).in(ArrayUtil.isNotEmpty(ids), BasicExpensesDviceConfigEntity::getId, ids));
	}
}